﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RRRSRoguelike.Screens;
using System.Diagnostics;

namespace RRRSRoguelike.Managers
{
    /// <summary>
    /// Manages the game flow.
    /// </summary>
    class GameManager
    {

        #region variables
        public Stopwatch Time;
        public Stopwatch GameTime;
        public List<Stopwatch> StopWatches = new List<Stopwatch>();
        public Stack<Screen> ScreenStack = new Stack<Screen>();

        //This should be a list in a screen manager.
        public DungeonScreen DungeonScreen;

        public InputManager InputManager;
        public SoundManager SoundManager;

        #endregion

        #region singleton
        static readonly GameManager instance = new GameManager();

        public static GameManager Instance
        {
            get { return instance; }
        }
        #endregion

        #region methods
        /// <summary>
        /// Initializes the gameScreens.
        /// </summary>
        private void Initialize() 
        {
            GameManager.Instance.SoundManager = new SoundManager();
            GameManager.Instance.InputManager = new InputManager();

            Time = new Stopwatch();
            Time.Start();
            GameTime = new Stopwatch();
            
            //Should be: 
            //splash screen
            DungeonScreen = new DungeonScreen();

            ScreenStack.Push(new TextScreen("Credits"));
            ScreenStack.Push(DungeonScreen);
            ScreenStack.Push(new TextScreen("Welcome"));
            ScreenStack.Push(new MenuScreen());
            ScreenStack.Push(new TextScreen("Splash"));
        }

        /// <summary>
        /// Entry point. Designs the gameflow.
        /// </summary>
        public void Run()
        {
            
            GameManager.Instance.Initialize();

            while (ScreenStack.Count>0)
                ScreenStack.Pop().Show();
        }

        public void Exit()
        {
            Environment.Exit(0);
        }

        public static void SetConsoleSize(int width, int height) 
        {
            //getting bigger
            if (width >= Console.WindowWidth && height >= Console.WindowHeight)
            { 
                Console.SetBufferSize(width, height);
                Console.SetWindowSize(width, height);
            }
            //getting smaller
            else if (width <= Console.WindowWidth && height <= Console.WindowHeight)
            {
                Console.SetWindowSize(width, height);
                Console.SetBufferSize(width, height);
            } 
            //width bigger height smaller
            else if (width >= Console.WindowWidth && height <= Console.WindowHeight)
            {
                Console.SetWindowSize(Console.WindowWidth, height);
                Console.SetBufferSize(width, height);
                Console.SetWindowSize(width, height);
            }
            //width smaller height bigger
            else if (width <= Console.WindowWidth && height >= Console.WindowHeight)
            {
                Console.SetWindowSize(width, Console.WindowHeight);
                Console.SetBufferSize(width, height);
                Console.SetWindowSize(width, height);
            }


        }

        #endregion

    }
}
